<?php

namespace App\Http\Controllers\Backend;


use App\Models\SiteAdv;
use App\Models\SiteCate;
use Illuminate\Support\Facades\Config;

class AdvController extends BaseController
{
    // 广告
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteAdv';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作


    // 返回列表数据
    public function lists()
    {
        $where = $this->listWhere();
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel($this->model);
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        if($where){
            $result = $model->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "id DESC");
        }

        // 用户头像，粉丝数量，关注数量，金币数
        if($result['list']){
            foreach($result['list'] as $key => $item){
                $result['list'][$key]['pic'] = $this->getFullUrl($item['pic']);
            }
        }

        return $this->success($result);
    }

    // 启用禁用
    public function status()
    {
        $_value = $this->getData('status', 0, 'intval');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');

        return $this->changeField('status', $_value);
    }

    // 广告位
    // TODO 暂未设置
    public function space()
    {
        // 获取广告数量
        $advDb = new SiteAdv();
        $lists = $advDb->selectRaw('space, count(id) number')->groupBy('space')->get()->toArray();
        $lists = array_column($lists, 'number', 'space');

        $result = [];
        $space  = config('site.adv_space');
        foreach($space as $key => $item){
            $result[] = ['title' => $item, 'id' => $key, 'number' => $lists[$key] ?? 0];
        }

        return $this->success($result);
    }

    //  获取栏目id
    // TODO 暂未设置
    public function cate()
    {
        $where   = [];
        $channel = $this->getData('channel', null, 'trim');
        if($channel){
            $where['channel'] = $channel;
        }

        $cateDb = new SiteCate();
        $cates  = $cateDb->isCache(true)->getAll($where);
        $cates  = array_values($cates);

        return $this->success($cates);
    }

    // 获取配置
    // TODO 暂未设置
    public function channel()
    {
        $list = Config::get('site.channel');
        $all  = [];
        foreach($list as $key => $item){
            $all[] = ['name' => $key, 'title' => $item];
        }

        return $this->success($all);
    }

    // 获取配置 将页面需要的配置合并在一起
    // TODO 暂未设置
    public function setting()
    {
        $cateDb  = new SiteCate();
        $cates   = $cateDb->getAll();
        $channel = Config::get('site.channel');
        $space   = config('site.adv_space');
        $all     = [];

        // 整理
        $all['cates'] = array_values($cates);
        foreach($space as $key => $item){
            $all['space'][] = ['title' => $item, 'space_name' => $key];
        }
        foreach($channel as $key => $item){
            $all['channel'][] = ['name' => $key, 'channel_name' => $item];
        }


        return $this->success($all);
    }

    // 列表查询
    protected function listWhere()
    {
        $where    = [];
        $space    = $this->getData('space', 0, 'intval');
        $cate_id  = $this->getData('cate_id', 0, 'intval');
        $terminal = $this->getData('terminal', null, 'trim');
        if($terminal){
            $where[] = ['terminal', '=', $terminal];
        }
        if($space){
            $where[] = ['space', '=', $space];
        }
        if($cate_id){
            $where[] = ['cate_id', '=', $cate_id];
        }

        return $where;
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            'title' => $this->getData('title', null, 'trim'),
            'space' => $this->getData('space', 0, 'intval'),
            'rank'  => $this->getData('rank', 0, 'intval'),

            'pic'      => $this->getData('pic', null, 'trim'),
            'url'      => $this->getData('url', null, 'trim'),
            'terminal' => $this->getData('terminal', 'app', 'trim'),
            'status'   => $this->getData('status', 1, 'intval'),
            //'cate_id'  => $this->getData('cate_id', 0, 'intval'),
            //'describe' => $this->getData('describe', null, 'trim'),
        ];


        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'title' => $this->getData('title', null, 'trim'),
            'space' => $this->getData('space', 0, 'intval'),
            'rank'  => $this->getData('rank', 0, 'intval'),

            'pic'      => $this->getData('pic', null, 'trim'),
            'url'      => $this->getData('url', null, 'trim'),
            'terminal' => $this->getData('terminal', 'app', 'trim'),
            //'status'   => $this->getData('status', 1, 'intval'),
        ];


        return $data;
    }

}


